# -*- coding: utf-8 -*-

from django.db.models import permalink
from konto.models import userModel
from django.db import models

from tinymce import models as tinymce_models

import datetime

class Kategoria(models.Model):

    nazwa = models.CharField(verbose_name='Nazwa Kategorii', max_length=200)
    slug = models.SlugField()
    
    class Meta:
	verbose_name='Kategoria'
	verbose_name_plural='Kategorie'
	
    def __unicode__(self):
        return self.nazwa
    
    @permalink
    def get_absolute_url(self):
        return ('blog.views.posty_kategoria', None, {
            'slug': self.slug})
    
class Tag(models.Model):

    nazwa = models.CharField(verbose_name='Tag', max_length=200)
    slug = models.SlugField()
  
    class Meta:
	verbose_name='Tag'
	verbose_name_plural='Tagi'
	ordering = ('nazwa',)
	
    def __unicode__(self):
        return self.nazwa
    
    @permalink
    def get_absolute_url(self):
        return ('blog.views.posty_tag', None, {
            'slug': self.slug})
	
class Komentarz(models.Model):
    
    autor = models.ForeignKey(userModel, verbose_name='Autor Komentarza')
    tresc = models.TextField(verbose_name='Treść')
    dataDodania= models.DateTimeField(verbose_name='Data dodania', default=datetime.datetime.now())
    
    def __unicode__(self):
	return u"%s(%s) - %s..." % (self.autor, self.dataDodania.date(), self.tresc[:20])
    
    class Meta:
        verbose_name='Komentarz'
	verbose_name_plural='Komentarze'
	ordering = ('dataDodania',)


class News(models.Model):
    
    tytul = models.CharField(verbose_name='Tytuł', max_length=200)
    autor = models.ForeignKey(userModel, verbose_name='Autor Postu')
    tresc = tinymce_models.HTMLField()
    kategoria = models.ForeignKey(Kategoria, verbose_name='Kategoria')
    czyNaGlowny = models.BooleanField(verbose_name='Czy Post ma się znalesc na głownym blogu?', default = False)
    czyKomentarz = models.BooleanField(verbose_name='Czy wolno komentować?', default = True)
    tagi = models.ManyToManyField(Tag, verbose_name='Tagi', blank=True, null = True)
    komentarze = models.ManyToManyField(Komentarz, verbose_name='Komentarze', blank=True, null=True)
    dataDodania = models.DateTimeField(verbose_name='Data dodania', default=datetime.datetime.now())
    dataEdycji = models.DateTimeField(verbose_name='Data edycji', blank=True, null=True)
    iloscEdycji = models.IntegerField(verbose_name='Liczba modyfikacji', default = 0)
    slug = models.SlugField()
    
    
    class Meta:
        verbose_name='Post'
	verbose_name_plural='Posty'
	unique_together = ("tytul", "tresc",)
        ordering = ('-dataDodania',)
    
    def __unicode__(self):
        return self.tytul
    
    @permalink
    def get_absolute_url(self):
        return ('blog.views.post', None, {
            'autor': self.autor.username,
	    'rok': self.dataDodania.year,
            'miesiac': "%02d" % self.dataDodania.month,
            'slug': self.slug })   

    def liczba_komentarzy(self):
	return len( self.komentarze.all())
        
    

    

